
import SwiftUI

let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].path
let mobileGestaltPath = documentsPath + "/com.apple.MobileGestalt.plist"
let coreDuetPath = documentsPath + "/interactionC.db"
let speeddialPath = documentsPath + "/com.apple.mobilephone.speeddial.plist"
let addressBookPath = documentsPath + "/AddressBook.sqlitedb"
let addressBookImagesPath = documentsPath + "/AddressBookImages.sqlitedb"

struct KV: Identifiable {
    let key: String
    let value: String

    var id: String {
        key + value
    }
}

struct ContentView: View {
    @State var rows = [KV]()
    @State var toggle = false

    var body: some View {
        NavigationView {
            if toggle {
                list()
            } else {
                list()
            }
        }.onAppear(perform: {
            let files: [String] = [
                "/var/containers/Shared/SystemGroup/systemgroup.com.apple.mobilegestaltcache/Library/Caches/com.apple.MobileGestalt.plist",
                "/var/mobile/Library/CoreDuet/People/interactionC.db",
                "/var/mobile/Library/CoreDuet/People/interactionC.db-wal",
                "/var/mobile/Library/CoreDuet/People/interactionC.db-shm",
                "/var/mobile/Library/Preferences/com.apple.mobilephone.speeddial.plist",
                "/var/mobile/Library/AddressBook/AddressBook.sqlitedb",
                "/var/mobile/Library/AddressBook/AddressBook.sqlitedb-wal",
                "/var/mobile/Library/AddressBook/AddressBook.sqlitedb-shm",
                "/var/mobile/Library/AddressBook/AddressBookImages.sqlitedb",
                "/var/mobile/Library/AddressBook/AddressBookImages.sqlitedb-wal",
                "/var/mobile/Library/AddressBook/AddressBookImages.sqlitedb-shm"
            ]
            getData(files: files, handler: { key, value in
                self.rows.append(KV(key: key, value: value))
            }, fileExtractionCompletion: {
                toggle.toggle()
            })
        })
    }

    func list() -> some View {
        List {
            if FileManager.default.fileExists(atPath: coreDuetPath) {
                NavigationLink("Core Duet", destination: CoreDuetView())
            }
            if FileManager.default.fileExists(atPath: speeddialPath) {
                NavigationLink("Speed Dial", destination: SpeedDialView())
            }
            if FileManager.default.fileExists(atPath: addressBookPath) {
                NavigationLink("Address Book", destination: AddressBookView())
            }
            if FileManager.default.fileExists(atPath: addressBookImagesPath) {
                NavigationLink("Address Book Images", destination: AddressBookImagesView())
            }
            if FileManager.default.fileExists(atPath: mobileGestaltPath) {
                NavigationLink("Mobile Gestalt", destination: MobileGestaltView())
            }
            ForEach(rows, content: { row in
                HStack {
                    Text(row.key)
                    Spacer()
                    Text(row.value)
                }
            })
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
